package com.liangzi.service_core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liangzi.liangzi_common.exception.Assert;
import com.liangzi.liangzi_common.result.R;
import com.liangzi.liangzi_common.result.ResponseEnum;
import com.liangzi.service_core.mapper.VisitBookingHistoryMapper;
import com.liangzi.service_core.mapper.VisitBookingMapper;
import com.liangzi.service_core.mapper.VisitMedicalCardMapper;
import com.liangzi.service_core.pojo.bo.SnowflakeManager;
import com.liangzi.service_core.pojo.entity.InnerDoctorWork;
import com.liangzi.service_core.mapper.InnerDoctorWorkMapper;
import com.liangzi.service_core.pojo.entity.VisitBooking;
import com.liangzi.service_core.pojo.entity.VisitBookingHistory;
import com.liangzi.service_core.pojo.entity.VisitMedicalCard;
import com.liangzi.service_core.pojo.vo.VisitBookingVO;
import com.liangzi.service_core.service.InnerDoctorWorkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 医生排班表 服务实现类
 * </p>
 *
 * @author zhj
 * @since 2021-12-13
 */
@Service
@Slf4j
public class InnerDoctorWorkServiceImpl extends ServiceImpl<InnerDoctorWorkMapper, InnerDoctorWork> implements InnerDoctorWorkService {


    @Autowired
    private VisitMedicalCardMapper visitMedicalCardMapper;

    @Autowired
    private SnowflakeManager snowflakeManager;

    @Autowired
    private InnerDoctorWorkMapper innerDoctorWorkMapper;

    @Autowired
    private VisitBookingMapper visitBookingMapper;


    @Autowired
    private VisitBookingHistoryMapper visitBookingHistoryMapper;

    @Override
    public String insertBooking(String userId,String innerDoctorWorkId, VisitBookingVO visitBookingVO) {
        // TODO 事务处理
        // 排班id  卡id
        // 添加一条预约记录
        QueryWrapper<VisitMedicalCard> visitMedicalCardQueryWrapper = new QueryWrapper<>();
        visitMedicalCardQueryWrapper.eq("user_id",userId);
        VisitMedicalCard visitMedicalCardServiceOne = visitMedicalCardMapper.selectOne(visitMedicalCardQueryWrapper);
        VisitBooking visitBooking = new VisitBooking();
        BeanUtils.copyProperties(visitBookingVO,visitBooking,VisitBooking.class);
        String medicalCardId = visitMedicalCardServiceOne.getMedicalCardId();
        if(medicalCardId == null){
            log.info("medicalCardId not can null");
        }
        visitBooking.setVisitMedicalCardId(medicalCardId);
        visitBooking.setInnerDoctorWorkId(innerDoctorWorkId);
        Long visitBookingId = null ;
        try {
            visitBookingId = snowflakeManager.nextValue();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Assert.notNull(visitBookingId, ResponseEnum.SNOW_FAKE_ID_ERROR);
        visitBooking.setBookingId(visitBookingId.toString());
        // 获取医生id
        InnerDoctorWork innerDoctorWork = innerDoctorWorkMapper.selectById(innerDoctorWorkId);
        visitBooking.setInnerDoctorId(innerDoctorWork.getDoctorId().toString());
        int isSaveBooking = visitBookingMapper.insert(visitBooking);
        // 预约次数加一
            VisitMedicalCard visitMedicalCard = visitMedicalCardMapper.selectById(medicalCardId);
            visitMedicalCard.setUpdateTime(null);
            Long medicalCardBookingCount = visitMedicalCard.getMedicalCardBookingCount();
            medicalCardBookingCount++;
            visitMedicalCard.setMedicalCardBookingCount(medicalCardBookingCount);
        int isBookingCountUpdate = visitMedicalCardMapper.updateById(visitMedicalCard);


        // 添加到预约历史记录
        VisitBookingHistory visitBookingHistory = new VisitBookingHistory();
        BeanUtils.copyProperties(visitBookingVO,visitBookingHistory,VisitBookingHistory.class);
        visitBookingHistory.setMedicalCardId(visitMedicalCardServiceOne.getMedicalCardId());
        visitBookingHistory.setInnerDoctorWorkId(innerDoctorWorkId);
        visitBookingHistory.setVisitBookingId(visitBookingId.toString());
        visitBookingHistory.setDoctorId(innerDoctorWork.getDoctorId().toString());
        visitBookingHistory.setRemark("用户点击预约");
        int isBookingHistory = visitBookingHistoryMapper.insert(visitBookingHistory);
        // 医生排班剩余预约人数-1
        Integer surplusNum = innerDoctorWork.getSurplusNum();
        if(surplusNum == 0){
            log.info("预约人数已达上限");
           return null;
        }
        surplusNum--;
        innerDoctorWork.setSurplusNum(surplusNum);
        int isUpdate = innerDoctorWorkMapper.updateById(innerDoctorWork);
        if(isSaveBooking >0 && isBookingHistory > 0 && isUpdate >0 && isBookingCountUpdate > 0){
            return visitBookingId.toString();
        }
        return null;
    }
}
